package com.project.ereader;

import java.util.Date;
import java.util.List;

import com.project.adapters.AdapterRssFeed;
import com.project.database.SchemaHelper;
import com.project.database.TableRss;
import com.project.enums.EnumWebsite;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class RSSFeedActivity extends Activity {
	
	private Spinner spinnerWebsite;
	private List<TableRss> rssCollection;
	private SchemaHelper schemaHelper;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rssfeed);
		schemaHelper = new SchemaHelper(this);
		initialView();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_rssfeed, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.create:
			 final Dialog dialog = new Dialog(this);
			 dialog.setContentView(R.layout.dialog_create_rssfeed);
			 dialog.setTitle("RSS Feed");
			 dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			 
			 final Spinner dialogSpinnerWebsite = (Spinner)dialog.findViewById(R.id.spinnerWebsite);
			 final EditText dialogTxtCategory = (EditText)dialog.findViewById(R.id.txtCategory);
			 final EditText dialogTxtDescription = (EditText)dialog.findViewById(R.id.txtDescription);
			 final EditText dialogTxtLink = (EditText)dialog.findViewById(R.id.txtLink);
			 Button btnOk = (Button)dialog.findViewById(R.id.btnOK);
			 
			 btnOk.setOnClickListener(new OnClickListener() {		
				@Override
				public void onClick(View v) {
					TableRss rssItem = new TableRss();
					rssItem.setWebsite(dialogSpinnerWebsite.getSelectedItem().toString());
					rssItem.setCategory(dialogTxtCategory.getText().toString());
					rssItem.setDescription(dialogTxtDescription.getText().toString());
					rssItem.setLink(dialogTxtLink.getText().toString());
					rssItem.setPubDate(new Date());
					schemaHelper.addTableRss(rssItem);
					dialog.dismiss();
				}
			});
			 
			
			ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, listWebsite());
			spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			dialogSpinnerWebsite.setAdapter(spinnerAdapter);
			 dialog.show();
			break;
		default:
			break;
		}
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.activity_rssfeed_context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item){
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		switch(item.getItemId()) {
	    case R.id.delete:
	    	Toast.makeText(this, String.valueOf(rssCollection.get(info.position).getId()), Toast.LENGTH_SHORT).show();
	    	schemaHelper.deleteTableRss(rssCollection.get(info.position).getId());
	        return true;
	    }
		
		return false;
	}
	
	private void initialView(){
		spinnerWebsite = (Spinner)findViewById(R.id.spinnerWebsite);
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listWebsite());
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerWebsite.setAdapter(spinnerAdapter);
		spinnerWebsite.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				
				String website = parent.getItemAtPosition(pos).toString();
				rssCollection = schemaHelper.listTableRssBy(website);
				final TableRss[] rssArray = new TableRss[rssCollection.size()];
				rssCollection.toArray(rssArray);
				AdapterRssFeed adapterRssFeed = new AdapterRssFeed(view.getContext(),
						R.layout.list_item_rssfeed, rssArray);
				RSSFeedActivity activity = (RSSFeedActivity)view.getContext();
				ListView listView = (ListView)activity.findViewById(R.id.listView);
				listView.setAdapter(adapterRssFeed);
				registerForContextMenu(listView);
				listView.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						String website = rssArray[position].getWebsite();
						String link = rssArray[position].getLink();
						String category = rssArray[position].getCategory();
						Intent intent = new Intent(view.getContext(),MainActivity.class);
						Bundle bundle=new Bundle();
						bundle.putString("website", website);
						bundle.putString("category", category);
						bundle.putString("link", link);
						intent.putExtras(bundle);
						startActivity(intent);
						
					}
				});		
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	
	
	private String[] listWebsite(){
		String[] webSiteCollection = new String[EnumWebsite.values().length];
		int i = 0;
		for(EnumWebsite action:EnumWebsite.values())
        {
			webSiteCollection[i] = String.valueOf(action);
			i++;
		}
		return webSiteCollection;
	}
}
